""" An Time-Based One-Time Password Algorithm

Based on RFC6238. Copyright (C) 2012  ouwenxing (ouwenxing@gmail.com)

Example:

>>> from pytotp import TOTP

"""

import math
from pyhotp import HOTP

__all__ = ["TOTP"]

# Default secret is form RFC6238 Appendix D
DEFAULT_SECRET = "12345678901234567890" #20bytes
DEFAULT_SECRET32 = "12345678901234567890123456789012" #32bytes
DEFAULT_SECRET64 = "1234567890123456789012345678901234567890123456789012345678901234" #64bytes

# The default number of digits in an HOPT value.
DEFAULT_DIGITS = 6
MIN_DIGITS = 6
MAX_DIGITS = 8


class TOTP(HOTP):
    
    def __init__(self, hashname = "sha1", 
                 secret = DEFAULT_SECRET, digits = DEFAULT_DIGITS, T0 = 0, X = 30):

        HOTP.__init__(self, hashname, secret, digits)
        
        self.T0 = T0    #the initial counter time
        self.X = X      #the time step
    
    def generateTOTP(self, time_sec):
        
        counter = int(math.floor((time_sec - self.T0)/self.X))
        
        return HOTP.generateOTP(self, counter)
        
if __name__ == "__main__":
    
    mytotp = TOTP('sha1', DEFAULT_SECRET, 8, 0, 30)
    otp1 = mytotp.generateTOTP(59)
    otp2 = mytotp.generateTOTP(1111111109L)
    
    print otp1, otp2
    
    mytotp = TOTP('sha256', "12345678901234567890123456789012", 8, 0, 30)
    otp1 = mytotp.generateTOTP(59)
    otp2 = mytotp.generateTOTP(1111111109L)
    
    print otp1, otp2







        

    
